<?php

/**
 * @file
 * Default theme implementation to display a term.
 *
 * Available variables:
 * - $name: (deprecated) The unsanitized name of the term. Use $term_name
 *   instead.
 * - $content: An array of items for the content of the term (fields and
 *   description). Use render($content) to print them all, or print a subset
 *   such as render($content['field_example']). Use
 *   hide($content['field_example']) to temporarily suppress the printing of a
 *   given element.
 * - $term_url: Direct URL of the current term.
 * - $term_name: Name of the current term.
 * - $classes: String of classes that can be used to style contextually through
 *   CSS. It can be manipulated through the variable $classes_array from
 *   preprocess functions. The default values can be one or more of the following:
 *   - taxonomy-term: The current template type, i.e., "theming hook".
 *   - vocabulary-[vocabulary-name]: The vocabulary to which the term belongs to.
 *     For example, if the term is a "Tag" it would result in "vocabulary-tag".
 *
 * Other variables:
 * - $term: Full term object. Contains data that may not be safe.
 * - $view_mode: View mode, e.g. 'full', 'teaser'...
 * - $page: Flag for the full page state.
 * - $classes_array: Array of html class attribute values. It is flattened
 *   into a string within the variable $classes.
 * - $zebra: Outputs either "even" or "odd". Useful for zebra striping in
 *   teaser listings.
 * - $id: Position of the term. Increments each time it's output.
 * - $is_front: Flags true when presented in the front page.
 * - $logged_in: Flags true when the current user is a logged-in member.
 * - $is_admin: Flags true when the current user is an administrator.
 *
 * @see template_preprocess()
 * @see template_preprocess_taxonomy_term()
 * @see template_process()
 *
 * @ingroup themeable
 */
global $boolean;
global $result;
global $exclude_node_id;

if(arg(0) == 'category') {

  if($boolean == FALSE) {
    //result of category
    $result = views_get_view_result('category', 'category_head_article');

    //result of event category
    if(empty($result)) {
      $result = views_get_view_result('category', 'event_category_head_article');
    }


    //result of jcode
    if(empty($result)) {
      $result = views_get_view_result('nodequeue_10', 'block'); //can change the nodequeue id to correct id

      //set the nodequeue node id to the array;
      $exclude_node_id[] = $result[0]->nid;

      $jcode_latest_articles_result = views_get_view_result('j_code_latest_articles', 'block');

      foreach ($jcode_latest_articles_result as $result_key => $result_value) {
        //set the latest 5 nodes to the array
        $exclude_node_id[] = $result_value->field_category_taxonomy_term_data_nid;
      }

    }

    $boolean = TRUE;
  }
 
  //if in jcode category, this will not excute
  //get exclude node id for category and event category
  if(!empty($result) && empty($exclude_node_id)) {
    if(isset($result[0]->field_category_taxonomy_term_data_nid)) {
      $exclude_node_id = $result[0]->field_category_taxonomy_term_data_nid;
    }

    if(isset($result[0]->field_event_category_taxonomy_term_data_nid)) {
      $exclude_node_id = $result[0]->field_event_category_taxonomy_term_data_nid;
    }
    
  }
}

?>
<?php if ($view_mode == 'category_listing'): ?>
  <?php 
    //call a function in custom module to get latest 3 articles
    $nodes = _get_articles_by_term_id($term->tid, $exclude_node_id);

    
  	//print the wrapper
  	print '<div class="category-wrapper">';
  	//display the term name
  	print '<h2 class="block-title"><span>' . $name . '</span></h2><div class="category-row">';

  	//loop through the nodes array
  	foreach ($nodes as $key => $nid) {
  		//load the node by nid
  		$node = node_load($nid);

  		//get the alias of the nide
  		$path = drupal_get_path_alias('node/'.$node->nid);

  		//get the field thumbnail
  		$thumbnail = field_get_items('node', $node, 'field_thumbnail');

  		// check if the thumbnail exists
  		if(!empty($thumbnail)) {
		   $field_thumbnail = field_view_value('node', $node, 'field_thumbnail', $thumbnail[0], array(
			    'type' => 'image',
			    'settings' => array(
			      'image_style' => 'article_thumbnail',
			      'image_link' => 'content',
			    ),
			));
		   
  			$thumbnail_html = drupal_render($field_thumbnail);

  			//print the thumbnail html
			print '<div class="row"><div class="node-thumbnail block-image"><p>'. $thumbnail_html .'</p></div>';
  		}
  		else {
  			if(!empty($node->field_article_content)) {
          $collection_thumbnail_html = '';

  				$field_collection_id = $node->field_article_content['und'][0]['value'];

  				//load the first filed collection
  				$field_collection = field_collection_item_load($field_collection_id);

          if(isset($field_collection->field_image['und'][0])) {
            $field_collection_image = $field_collection->field_image['und'][0];

            $collection_thumbnail = array(
            'style_name' => 'article_thumbnail',
            'path' => $field_collection_image['uri'],
            'width' => '',
            'height' => '',
            'alt' => $field_collection_image['alt'],
            'title' => $field_collection_image['title'],
            );

            $collection_thumbnail_html = theme('image_style', $collection_thumbnail);
          }
  				
          //print opening row
          print '<div class="row">';
				
				  //print the field collection thumbnail html
          if(!empty($collection_thumbnail_html)) {
            print '<div class="node-thumbnail block-image"><p><a href="/'. $path .'">' . $collection_thumbnail_html .'</a></p></div>';
          }
          
  			}
  		}

  		//print the node title html
  		print '<h4 class="title"><a href="/'. $path .'">'. $node->title .'</a></h4></div>';
  	}

  	//print the See More button
  	print '</div><div><a class="see-more-button jessica-btn" href="'. $term_url .'">SEE MORE</a></div>';

  	//print the closing wrapper
  	print '</div>';

  ?>
  
<!-- if it's not category_listing view mode -->
<?php else: ?>
<div id="taxonomy-term-<?php print $term->tid; ?>" class="<?php print $classes; ?>">

  <?php if (!$page): ?>
    <h2><a href="<?php print $term_url; ?>"><?php print $term_name; ?></a></h2>
  <?php endif; ?>

  <div class="content">
    
    <?php print render($content); ?>
  </div>

</div>
<?php endif; ?>